<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="show ? rowObj.T1 : rowObj.F1" :xs="show ? rowObj.T2 : rowObj.F2">
        <workSiteLeft ref="workSiteLeft" :show="show" @offerDeptTree="offerDeptTree" @showLeft="showLeft" @queryVal="queryVal" />
      </el-col>

      <el-col :span="show ? rowObj.T3 : rowObj.F3" :xs="show ? rowObj.T4 : rowObj.F4">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-button style="margin: 3px 12px 0 0;" size="mini" :icon="show ? 'el-icon-s-fold' : 'el-icon-s-unfold'" circle @click="showLeft(show)"></el-button>
          <!-- <el-form-item prop="baseCode" label="工地名称">
            <el-select v-model="queryParams.baseCode" clearable size="mini" placeholder="请选择标段">
              <el-option v-for="item in constructionList" :key="item.code" :label="item.name" :value="item.code"></el-option>
            </el-select>
          </el-form-item> -->

          <el-form-item label="设备开机日期" prop="bootTime" label-width="200">
            <deatetime ref="deatetime" v-model="queryParams.time"></deatetime>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" v-hasPermi="['xtcsgl:disconnectRecord:query']">搜索</el-button>
            <!-- <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button> -->
          </el-form-item>
        </el-form>

        <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
          <el-tab-pane label="试验室" name="first">
            <!-- <el-row :gutter="10" class="mb8"><right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar></el-row> -->
            <el-table border v-loading="loading" :data="siteDisconnectRecordList">
              <el-table-column label="归属单位" align="center" prop="deptName" show-overflow-tooltip min-width="100" />
              <el-table-column label="设备名称" align="center" prop="deviceName" show-overflow-tooltip min-width="200" />
              <el-table-column label="设备型号" align="center" prop="model" show-overflow-tooltip min-width="100" />
              <el-table-column label="归属试验室" align="center" prop="laboratoryName" show-overflow-tooltip min-width="100" />
              <el-table-column label="开机次数" align="center" prop="bootCount" show-overflow-tooltip min-width="100" />
              <el-table-column label="断网时长(分)" align="center" prop="totalDuration" show-overflow-tooltip min-width="100" />
              <el-table-column label="设备状态" align="center" show-overflow-tooltip min-width="130">
                <template slot-scope="scope">
                  <el-tag :type="scope.row.deviceStatus === '0' ? 'success' : scope.row.deviceStatus === '1' ? 'info' : 'info'"> {{ scope.row.deviceStatusName }}</el-tag>
                </template>
              </el-table-column>
              <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
                <template slot-scope="scope">
                  <!-- <el-button size="mini" type="text" icon="el-icon-notebook-1" v-show="scope.row.deviceStatus != '2'" @click="handleOnline(scope.row)">实时记录</el-button> -->
                  <el-button size="mini" type="text" icon="el-icon-notebook-2" @click="handleHistory(scope.row)" v-hasPermi="['xtcsgl:disconnectRecord:details']">查看详情</el-button>
                </template>
              </el-table-column>
            </el-table>
            <pagination class="fenye" v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
          </el-tab-pane>

          <el-tab-pane label="拌合站" name="second">
            <el-table border v-loading="loading" :data="siteBhzDisconnectRecordList">
              <el-table-column label="归属单位" align="center" prop="deptName" show-overflow-tooltip min-width="100" />
              <el-table-column label="拌合站名称" align="center" prop="deviceName" show-overflow-tooltip min-width="200" />
              <el-table-column label="开机次数" align="center" prop="bootCount" show-overflow-tooltip min-width="100" />
              <el-table-column label="断网时长(分)" align="center" prop="totalDuration" show-overflow-tooltip min-width="100" />
              <el-table-column label="设备状态" align="center" show-overflow-tooltip min-width="130">
                <template slot-scope="scope">
                  <el-tag :type="scope.row.deviceStatus === '0' ? 'success' : scope.row.deviceStatus === '1' ? 'info' : 'info'"> {{ scope.row.deviceStatusName }}</el-tag>
                </template>
              </el-table-column>
              <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
                <template slot-scope="scope">
                  <!-- <el-button size="mini" type="text" icon="el-icon-notebook-1" v-show="scope.row.deviceStatus != '2'" @click="handleOnline(scope.row)">实时记录</el-button> -->
                  <el-button size="mini" type="text" icon="el-icon-notebook-2" @click="handleHistory(scope.row)" v-hasPermi="['xtcsgl:disconnectRecord:details']">查看详情</el-button>
                </template>
              </el-table-column>
            </el-table>
            <pagination class="fenye" v-show="totalBhz > 0" :total="totalBhz" :page.sync="bhzQueryParams.pageNum" :limit.sync="bhzQueryParams.pageSize" @pagination="getBhzList" />
          </el-tab-pane>
        </el-tabs>
      </el-col>
    </el-row>

    <!-- 历史记录dialog -->
    <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body>
      <el-table border v-loading="hisloading" :data="historyRecordList" size="mini">
        <el-table-column type="index" width="50" align="center"></el-table-column>
        <el-table-column label="开机时间" align="center" prop="bootTime" width="135" />
        <el-table-column label="关机时间" align="center" width="135">
          <template slot-scope="scope">
            {{ scope.row.unattendedTime != null ? scope.row.unattendedTime : scope.row.isNormalShutdown === '采集程序异常关闭' ? '-' : '暂无数据' }}
          </template>
        </el-table-column>
        <el-table-column label="设备状态" align="center" min-width="130">
          <template slot-scope="scope">
            <el-tag :type="scope.row.isNormalShutdown === '正常关机' ? 'success' : scope.row.isNormalShutdown === '采集程序异常关闭' ? 'danger' : 'info'"> {{ scope.row.isNormalShutdown }}</el-tag>
          </template>
        </el-table-column>
        <!-- <el-table-column label="断联次数" align="center" prop="recordCount" min-width="90">
          <template slot-scope="scope">
            <p style="color:orangered;font-weight: 600;" v-show="scope.row.recordCount > 0">{{ scope.row.recordCount }}</p>
            <p v-show="scope.row.recordCount == 0">{{ scope.row.recordCount }}</p>
          </template>
        </el-table-column> -->
         <el-table-column label="断联次数" align="center" prop="recordCount" min-width="90">
          <template slot-scope="scope">
            <span style="color:orangered;font-weight: 600;" v-show="scope.row.recordCount > 0">{{ scope.row.recordCount }}</span>
            <span v-show="scope.row.recordCount == 0">{{ scope.row.recordCount }}</span>
          </template>
        </el-table-column>
        <el-table-column label="断联时长(分)" align="center" prop="disconnectDuration" min-width="100" />
        <el-table-column label="设备运行时长(分)" align="center" min-width="130">
          <template slot-scope="scope">{{ scope.row.bootDuration == null ? '-' : scope.row.bootDuration }}</template>
        </el-table-column>
        <el-table-column label="联网时长占比" align="center" prop="bootProportion" min-width="110">
          <template slot-scope="scope">{{ scope.row.bootProportion == null ? '-' : scope.row.bootProportion }}</template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="90">
          <template slot-scope="scope">
            <el-button size="mini" type="text" v-show="scope.row.recordCount != '0'" @click="handleHisDetail(scope.row)" v-hasPermi="['xtcsgl:disconnectRecord:edit']">断联详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="historytotal > 0" :total="historytotal" :page.sync="historyQueryParams.pageNum" :limit.sync="historyQueryParams.pageSize" @pagination="getHistoryList" />

      <!-- 断网记录详情 -->
      <el-dialog width="60%" title="详情" :visible.sync="innerVisible" append-to-body>
        <el-table border v-loading="innerloading" :data="historyDetailRecordList" size="mini" max-height="450">
          <el-table-column type="index" width="50" align="center"></el-table-column>
          <el-table-column label="开机时间" align="center" prop="bootTime" />
          <el-table-column label="关机时间" align="center">
            <template slot-scope="scope">{{ scope.row.unattendedTime != null ? scope.row.unattendedTime : '-' }}</template>
          </el-table-column>
          <el-table-column label="断网开始时间" align="center" prop="disconnectBegin" />
          <el-table-column label="断网结束时间" align="center" prop="disconnectEnd" />
          <el-table-column label="断联时长(分)" align="center" prop="disconnectDuration" />
          <!-- <el-table-column label="设备状态" align="center">
            <template slot-scope="scope">
              <el-tag :type="scope.row.isNormalShutdown === '正常关机' ? 'success' : scope.row.isNormalShutdown === '采集程序异常关闭' ? 'danger' : 'info'">{{scope.row.isNormalShutdown}}</el-tag>
            </template>
          </el-table-column> -->
        </el-table>
      </el-dialog>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSiteDisconnectRecord, listSiteDisconnectBhzRecord, getSiteDisconnectRecord, deptListData, getSiteDisconnectDetail, getSiteOnlineRecord } from "@/api/xtcsgl/siteDisconnectRecord";
import deatetime from "@/components/Datetime"; // 施工日期
import workSiteLeft from "@/views/components/workSiteLeft";
export default {
  name: "SiteDisconnectRecord",
  data() {
    return {
      deptOptions: undefined,
      show: true,
      rowObj: {
        T1: 3,
        T2: 24,
        T3: 21,
        T4: 24,
        F1: 0,
        F2: 0,
        F3: 24,
        F4: 24,
      },
      //切换TAB
      activeName: 'first',
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      hisloading: true,
      innerVisible: false,
      innerloading: true,
      onlineloading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      totalBhz: 0,
      // 工地现场断网记录表格数据
      siteDisconnectRecordList: [],
      // 拌合站现场断网记录表格数据
      siteBhzDisconnectRecordList: [],
      //历史记录列表
      historyRecordList: [],
      //历史记录详情列表
      historyDetailRecordList: [],
      //实时记录详情列表
      onlineRecordList: [],
      //历史记录总条数
      historytotal: 0,
      // 弹出层标题
      title: "",
      // 在线弹出层
      onlineTitle: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层
      olineOpen: false,
      constructionList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: undefined,
        sourceId: undefined,
        groupId: undefined,
        groupNo: undefined,
        bootTime: undefined,
        disconnectBegin: undefined,
        disconnectEnd: undefined,
        disconnectDuration: undefined,
        unattendedTime: undefined,
        send: undefined,
        isNormalShutdown: undefined,
        createDate: undefined
      },
      historyQueryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      bhzQueryParams: {
        pageNum: 1,
        pageSize: 10,
      },
    };
  },
  components: {
    deatetime,
    workSiteLeft
  },
  created() {
    this.getProjectList()
    this.getList();
    this.getBhzList();
  },
  methods: {
    /** 查询工地现场断网记录列表 */
    getList() {
      this.loading = true;
      listSiteDisconnectRecord(this.queryParams).then(response => {
        this.siteDisconnectRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getBhzList() {
      this.loading = true;
      listSiteDisconnectBhzRecord(this.bhzQueryParams).then(response => {
        this.siteBhzDisconnectRecordList = response.rows;
        this.totalBhz = response.total;
        this.loading = false;
      });
    },
    /** 查询工地现场断网历史记录列表 */
    getHistoryList() {
      this.hisloading = true;
      getSiteDisconnectRecord(this.historyQueryParams).then(response => {
        this.hisloading = false;
        this.historyRecordList = response.rows;
        this.historytotal = response.total;
      });
    },
    offerDeptTree(deptOptions) {
      this.deptOptions = deptOptions;
    },
    // 左侧树点击事件
    showLeft(show) {
      this.show = !show;
    },
    queryVal(val) {
      this.queryParams.baseCode = val.code;
      this.handleQuery();
    },
    //切换按钮
    handleClick(tab, event) {
      console.log(tab, event);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 左侧树点击事件
    showLeft(show) {
      this.show = !show;
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        type: undefined,
        sourceId: undefined,
        groupId: undefined,
        groupNo: undefined,
        bootTime: undefined,
        disconnectBegin: undefined,
        disconnectEnd: undefined,
        disconnectDuration: undefined,
        unattendedTime: undefined,
        send: undefined,
        isNormalShutdown: undefined,
        createDate: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if (this.activeName == 'first') {
        this.queryParams = this.getSearchParams();
        this.queryParams.pageNum = 1;
        this.getList();
      } else {
        this.bhzQueryParams = this.getSearchParams();
        this.bhzQueryParams.pageNum = 1;
        this.getBhzList();
      }
    },
    getSearchParams() {
      const params = {};
      for (const key in this.queryParams) {
        if (key === "time") {
          const time = this.queryParams[key];
          if (time && time.length === 2) {
            params.startTime = time[0];
            params.endTime = time[1];
          }
        } else {
          params[key] = this.queryParams[key];
        }
      }
      return params;
    },
    /** 查询工地名称列表 */
    getProjectList() {
      deptListData().then((response) => {
        this.constructionList = response;
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs.deatetime.startTime = null;
      this.$refs.deatetime.endTime = null;
      this.queryParams.time = [];
      this.queryParams.startTime = "";
      this.queryParams.endTime = "";
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工地现场断网记录";
    },
    /** 查询工地现场断网历史记录详情 */
    handleHisDetail(row) {
      this.innerVisible = true
      this.innerloading = true
      let params = {}
      params.groupId = row.groupId
      getSiteDisconnectDetail(params).then(response => {
        this.historyDetailRecordList = response.rows;
        this.innerloading = false
      });
    },
    /** 历史记录查看按钮操作 */
    handleHistory(row) {
      this.hisloading = true;
      this.reset();
      this.historyQueryParams.type = row.type
      this.historyQueryParams.startTime = this.queryParams.startTime
      this.historyQueryParams.endTime = this.queryParams.endTime
      getSiteDisconnectRecord(this.historyQueryParams).then(response => {
        this.hisloading = false;
        this.historyRecordList = response.rows;
        this.historytotal = response.total;
        this.open = true;
        this.title = row.deptName + "【" + row.deviceName + "】网络断联记录";
      });
    },
  }
};
</script>

<style scoped>
.fenye {
  width: 100%;
  height: 80px;
}
</style>

